package com.csw.encryptiontechnology.utils.rsa.jiaMi;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

@Component
@Slf4j
public class RSAUtils {

    public static String publicKeyStr;

    public static String privateKeyStr;

    public RSAUtils(@Value("${rsa.secretKey.publicKey}") String publicKeyStr,
                    @Value("${rsa.secretKey.privateKey}") String privateKeyStr) {
        RSAUtils.publicKeyStr = publicKeyStr;
        RSAUtils.privateKeyStr = privateKeyStr;
    }

    private static int kEY_LENGTH = 1024;


    public static void main(String[] args) {
        try {
            // 创建密钥对生成器，指定 RSA 算法
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            // 设置密钥长度
            keyPairGenerator.initialize(kEY_LENGTH);
            // 生成密钥对
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            // 获取公钥和私钥
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
            // 将公钥和私钥转换为 Base64 编码的字符串以便输出
            String publicKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());
            String privateKeyStr = Base64.getEncoder().encodeToString(privateKey.getEncoded());
            System.out.println("公钥：" + publicKeyStr);
            System.out.println("私钥：" + privateKeyStr);
            //测试加密和解密1
            String s = encrypt("张三安徽的人123456", publicKeyStr);
            System.out.println("加密后]" + s);
            String decrypt = decrypt(s, privateKeyStr);
            System.out.println("解密后]" + decrypt);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String plainText, String publicKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException, Exception {
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedText, String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException, Exception {
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }

    public static String encrypt(String plainText) throws NoSuchAlgorithmException, InvalidKeySpecException, Exception {
        return encrypt(plainText, publicKeyStr);
    }

    public static String decrypt(String encryptedText) throws NoSuchAlgorithmException, InvalidKeySpecException, Exception {
        return decrypt(encryptedText, privateKeyStr);
    }


}